package android.slc.appbase.ui.vd;

import android.os.Bundle;
import android.slc.appbase.utils.AppIntentUtils;
import android.slc.core.vm.SingleLiveEvent;

import androidx.databinding.ObservableField;

import java.io.Serializable;

/**
 * 详细内容基础类
 *
 * @author slc
 * @date 2020-08-25 16:13
 */
public abstract class BaseCommonDetailsShelfVmBox<T extends Serializable> extends BaseDataChangeVmBox implements BaseRegisterActivityFunc.IIntegrationRegisterActivityFunc {
    public ObservableField<T> dataOf = new ObservableField<>();
    public SingleLiveEvent<Bundle> showEditUiOf = new SingleLiveEvent<>();

    private BaseRegisterActivityFunc.IntentTargetWrapper intentTargetWrapper;
    public final BaseRegisterActivityFunc<Serializable> editRegisterActivityFunc;

    public BaseCommonDetailsShelfVmBox() {
        this.editRegisterActivityFunc = createActionRegisterActivityFunc();
        if (this.editRegisterActivityFunc != null && this.editRegisterActivityFunc.getOnTargetActivityResultCallback() == null) {
            this.editRegisterActivityFunc.setOnActivityResultCallback(new SimpleRegisterActivityFunc.OnActivityResultCallback<Serializable>() {
                @Override
                public void onIntentData(Serializable data) {
                     BaseCommonDetailsShelfVmBox.this.notifyRefreshByGlobal(data);
                     BaseCommonDetailsShelfVmBox.this.fillResult(AppIntentUtils.getBundleByData(data));
                }

                @Override
                public void onIntentBundle(Bundle data) {
                    BaseCommonDetailsShelfVmBox.this.onActivityResult(data);
                }
            });
        }
    }

    @Override
    public BaseRegisterActivityFunc<Serializable> createActionRegisterActivityFunc() {
        return new SimpleRegisterActivityFunc<>(getGotoTargetActivityItw());
    }

    /**
     * 获取item点击后要取得activityClass
     *
     * @return
     */
    @Override
    public final BaseRegisterActivityFunc.IntentTargetWrapper getGotoTargetActivityItw() {
        if (intentTargetWrapper == null) {
            intentTargetWrapper = createGotoTargetActivityItw();
        }
        return intentTargetWrapper;
    }

    protected BaseRegisterActivityFunc.IntentTargetWrapper createGotoTargetActivityItw() {
        return null;
    }
    /**
     * 通知刷新
     *
     * @param data
     */
    protected void notifyRefresh(T data) {
        if (data != null) {
            dataOf.set(data);
        }
    }

    /**
     * 显示编辑界面
     */
    public void showEditUi() {
        showEditUiOf.setValue(AppIntentUtils.getBundleByData(dataOf.get()));
    }

}
